import { Injectable } from '@nestjs/common'
import { InjectModel } from '@nestjs/mongoose'
import { Model } from 'mongoose'
import { BlogPost } from './schemas/blog-post.schema'
import { CreateBlogPostDto } from './dto/create-blog-post.dto'

@Injectable()
export class BlogService {
  constructor(
    @InjectModel(BlogPost.name) private blogPostModel: Model<BlogPost>
  ) {}

  async findAll(): Promise<BlogPost[]> {
    return this.blogPostModel.find().exec()
  }

  async findOne(id: string): Promise<BlogPost> {
    return this.blogPostModel.findById(id).exec()
  }

  async create(createBlogPostDto: CreateBlogPostDto): Promise<BlogPost> {
    const createdPost = new this.blogPostModel(createBlogPostDto)
    return createdPost.save()
  }
}